/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.songcha.module_bookreader;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
//import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.songcha.library_business.bean.advert.AdvertInfoBean;
import com.songcha.library_business.config.FunctionConfig;
import com.songcha.library_business.config.VarConfig;
import com.songcha.library_business.helper.AdvertHelper;
import com.songcha.library_common.LibraryCommon;
import com.songcha.library_common.util.ToastUtil;
import com.songcha.module_bookreader.base.Constant;
//import com.songcha.module_bookreader.component.AppComponent;
//import com.songcha.module_bookreader.component.DaggerAppComponent;
//import com.songcha.module_bookreader.module.AppModule;
//import com.songcha.module_bookreader.module.BookApiModule;
import com.songcha.module_bookreader.utils.AppUtils;
import com.songcha.module_bookreader.utils.LogUtils;
import com.songcha.module_bookreader.utils.SharedPreferencesUtil;
import com.songcha.library_business.provider.IApplicationInitProvider;
import com.songcha.library_database_douyue.LibraryDatabaseDouyueApplication;

import java.util.ArrayList;
//import com.pansy.library_common.provider.IApplicationContextProvider;



@Route(path="/bookreader/AppInitImpl")
 public class ModuleBookReaderApplication extends Application implements IApplicationInitProvider {

    //private static AppComponent appComponent;
    private static Context mContext;

    //private RefWatcher refWatcher;

    //public static RefWatcher getRefWatcher(Context context) {
       // ReaderApplication application = (ReaderApplication) context.getApplicationContext();
        //return application.refWatcher;
    //}


    @Override
    public void onCreate() {
        super.onCreate();

        if(BuildConfig.DEBUG){
            ARouter.openLog();
            ARouter.openDebug();
        }
        ARouter.init(this);

        LibraryCommon.init(this);
        LibraryDatabaseDouyueApplication.init(this);

        /*AdvertHelper.initAdvertSdk(this,"5437150",new TTAdSdk.Callback() {
            @Override
            public void success() {

            }

            @Override
            public void fail(int i, String s) {

            }
        });*/
        //TODO
        FunctionConfig.Companion.setIS_ADVERT_ENABLED(true);
        VarConfig.Companion.setADVERT_READER_CHAPTER_XXL_INFO(new AdvertInfoBean(1,10, false,
                new AdvertInfoBean.CodeIdsBean(new ArrayList<>(),new ArrayList<>())));

        init(this);

    }

    public static void init(Application context) {
        mContext=context;

        //initComponent();
        AppUtils.init(context);
        initPrefs();
        initNightMode();

    }

    public static Context getContext() {
        return mContext;
        //return sInstance;
        /*if(mContext==null){
            mContext=((IApplicationContextProvider)ARouter.getInstance().build("/business/context").navigation()).getContext();
        }
        return mContext;*/
    }

    private static void initComponent() {
       /* appComponent = DaggerAppComponent.builder()
                .bookApiModule(new BookApiModule())
                .appModule(new AppModule(mContext))
                .build();*/
    }

    /*public static AppComponent getAppComponent() {
        return appComponent;
    }*/


    private static void initPrefs() {
        SharedPreferencesUtil.init(mContext, mContext.getPackageName() + "_preference", Context.MODE_MULTI_PROCESS);
    }

    private static void initNightMode() {
        boolean isNight = SharedPreferencesUtil.getInstance().getBoolean(Constant.ISNIGHT, false);
        //TODO
        LogUtils.d("isNight=" + isNight);
        if (isNight) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    }


    @Override
    public void initApplication(@NonNull Application application) {
        init(application);
    }

    @Override
    public void init(Context context) {

    }
}
